package com.zwc.kotlin.coroutines

import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

suspend fun delay(time: Long, unit: TimeUnit = TimeUnit.MILLISECONDS) {
    if (time <= 0) {
        return
    }

    suspendCoroutine<Unit> {
        executor.schedule({
            it.resume(Unit)
        }, time, unit)
    }
}

private val executor = Executors.newScheduledThreadPool(1) {
    Thread(it, "Scheduler").apply {
        isDaemon = true
    }
}